var SceneGame = require('SceneGame');
import { EventKey } from '../common/Key';
import { GameState } from './GameState';
import { AnimalDatas, JumpSpace } from './GameDefine';
cc.Class({
    extends: cc.Component,

    properties: {
        nodCore: cc.Node,
        sprAnimal: cc.Sprite,
    },

    onEnable() {
        this._canCountdown = true;
        this.node.on('touchstart', this._onTouchStart, this);
        this.node.on('touchmove', this._onTouchMove, this);
        this.node.on('touchend', this._onTouchEnd, this);
        this.node.on('touchcancel', this._onTouchCancel, this);
    },

    onDisable() {
        this._canCountdown = false;
        this.node.off('touchstart', this._onTouchStart, this);
        this.node.off('touchmove', this._onTouchMove, this);
        this.node.off('touchend', this._onTouchEnd, this);
        this.node.off('touchcancel', this._onTouchCancel, this);
        this.node.stopAllActions();
    },

    init(index, canJump = true) {
        this._time = 0;
        this._canJump = canJump;
        this._index = index;
        this.node.stopAllActions();
        this._resetAnimal();
        this._time = 0;

        this.scheduleOnce(() => {
            this._jump();
        }, 1);

        let data = AnimalDatas[index] || { icon: '1001' };
        let url = 'textures/animals/' + data.icon;
        G.common.setSpriteFrame(this.sprAnimal, url, () => {
            this.node.width = this.sprAnimal.node.width * Math.abs(this.nodCore.scaleX);
            this.node.height = this.sprAnimal.node.height * Math.abs(this.nodCore.scaleY);
        });
    },

    getIndex() {
        return this._index;
    },

    _onTouchStart(event) {
        this._canCountdown = false;
        this._time = 0;
        this.node.stopAllActions();
        this._resetAnimal();

        let touchPos = this.node.parent.convertToNodeSpaceAR(event.getLocation());
        this.offsetPos = touchPos.sub(this.node.position);
    },
    _onTouchMove(event) {
        let touchPos = this.node.parent.convertToNodeSpaceAR(event.getLocation());
        touchPos = touchPos.sub(this.offsetPos);
        if (touchPos.x > this.node.parent.width / 2) {
            touchPos.x = this.node.parent.width / 2;
        }
        if (touchPos.x < -this.node.parent.width / 2) {
            touchPos.x = -this.node.parent.width / 2;
        }
        if (touchPos.y > this.node.parent.height / 2) {
            touchPos.y = this.node.parent.height / 2;
        }
        if (touchPos.y < -this.node.parent.height / 2) {
            touchPos.y = -this.node.parent.height / 2;
        }

        this.node.x = touchPos.x;
        this.node.y = touchPos.y;
    },
    _onTouchEnd(event) {
        this._canCountdown = true;
        G.onFire.fire(EventKey.EVENT_TRY_COMPOSE_ANIMAL, { animal: this });
    },
    _onTouchCancel(event) {
        this._canCountdown = true;
        G.onFire.fire(EventKey.EVENT_TRY_COMPOSE_ANIMAL, { animal: this });
    },

    _resetAnimal() {
        this.node.scale = 1;
    },

    // 生成poop
    _genPoop() {
        let node = G.utils.getPoolNode('nodePoolPoop');
        node.parent = SceneGame.instance.nodPoopParent;
        node.position = this.node.position;
        let num = GameState.getAnimalProduction(this._index);
        node.getComponent('ItemPoop').init(num);
    },

    _jump() {
        if (!this._canJump) {
            return;
        }
        // 获取目标点
        this._getTargetPosCount = 0;
        let targtPos = this._getTargetPos();
        if (targtPos.x > this.node.x) {
            this.nodCore.scaleX = -1 * Math.abs(this.nodCore.scaleX);
        } else {
            this.nodCore.scaleX = 1 * Math.abs(this.nodCore.scaleX);
        }
        this._genPoop();
        let action = cc.sequence(
            cc.scaleTo(0.15 * GameState.getSpeedScale(), 1.1, 0.7),
            cc.spawn(
                cc.scaleTo(0.4 * GameState.getSpeedScale(), 0.9, 1.2),
                cc.moveTo(0.4 * GameState.getSpeedScale(), targtPos),
            ),
            cc.scaleTo(0.1 * GameState.getSpeedScale(), 1.1, 0.8),
            cc.scaleTo(0.1 * GameState.getSpeedScale(), 1, 1),
            cc.callFunc(() => {
                G.onFire.fire(EventKey.EVENT_UPDATE_ANIMAL_ZINDEX);
            })
        )
        this.node.runAction(action);
    },

    _getTargetPos() {
        this._getTargetPosCount++;
        // 获取目标点
        let speed = 100;
        // 获取方向
        let angle = Math.random() * 360;
        var dir = cc.v2(Math.cos(angle), Math.sin(angle));
        //单位化向量
        dir.normalizeSelf();
        let targetPos = cc.v2(this.node.x + dir.x * speed, this.node.y + dir.y * speed);

        // 判断是否可移动
        if (targetPos.x > this.node.parent.width / 2 || targetPos.x < -this.node.parent.width / 2
            || targetPos.y > this.node.parent.height / 2 || targetPos.y < -this.node.parent.height / 2) {
            if (this._getTargetPosCount >= 5) {
                return this.node.position;
            }
            return this._getTargetPos();
        }
        return targetPos
    },

    recycle() {
        G.utils.putPoolNode('nodePoolAnimal', this.node);
    },

    update(dt) {
        if (!this._canCountdown) {
            return;
        }
        this._time += dt;
        if (this._time >= JumpSpace * GameState.getSpeedScale()) {
            this._time = 0;
            this._jump();
        }
    },
});
